﻿
@{
    ViewBag.Title = "告警列表";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

<link rel="stylesheet" href="~/Content/css/swiper.min.css">
<link rel="stylesheet" href="~/Content/css/swiper.css" />
<link rel="stylesheet" href="~/Content/css/dropload.css" />
<link rel="stylesheet" href="~/Content/css/mobileSelect.css" />
<link rel="stylesheet" href="~/Content/css/easyui.css" />
<script src="~/Content/script/mobileSelect.js"></script>
<script src="~/Content/script/swiper.min.js"></script>
<script src="~/Content/script/dropload.min.js"></script>
<script src="~/Content/script/jquery.easyui.min.js"></script>
@*<script src="~/Content/script/jquery-1.8.3.min.js"></script>*@
<style>
    * {
        padding: 0;
        margin: 0;
    }

    html, body {
        position: relative;
        height: 100%;
    }

    li, a {
        list-style: none;
        text-decoration: none;
    }

    .swiper-container {
        width: 100%;
    }

    #nav {
        height: 40px;
        background: #fff;
    }

        #nav .swiper-wrapper {
            margin: 0;
            height: 40px;
            line-height: 40px;
        }

        #nav li {
            text-align: center;
            color: #474747;
        }

            #nav li span {
                color: red;
            }

        #nav .active-nav {
            color: #55A5C3;
        }

    #tableHeader {
        height: 40px;
    }

        #tableHeader table {
            width: 100%;
            height: 40px;
            color: #fff;
            font-size: 15px;
        }

            #tableHeader table th:nth-child(even) {
                background: #4FC2A1;
            }

            #tableHeader table th:nth-child(odd) {
                background: #334960;
            }


    #page {
        position: absolute;
        top: 125px;
        left: 0;
        right: 0;
        bottom: 55px;
        overflow: hidden;
    }

        #page .swiper-slide {
            text-align: center;
            font-size: 30px;
            overflow-y: scroll;
            position: relative;
        }

    .dropload-down {
        display: none;
    }

    #page table {
        background: #fff;
    }

    #page .swiper-slide table {
        width: 100%;
    }

    #page table tr:nth-child(even) {
        background: #F8F8F8;
    }

    #page tr td {
        font-size: 16px;
        color: #000;
        padding: 10px 0;
        font-family: "微软雅黑";
    }

        #page tr td:nth-child(1), #page tr td:nth-child(3), #page tr td:nth-child(4) {
            width: 20%;
        }

        #page tr td:nth-child(2) {
            width: 40%;
        }

    #seacher {
        display: flex;
        justify-content: space-between;
        padding: 6px 8px;
        background: #E3E3E5;
        height: 33px;
        box-sizing: content-box
    }

        #seacher select {
            width: 25%;
            border-radius: 5px;
            border: none;
            outline: none;
            box-sizing: border-box;
            background: #fff;
            padding-left: 6px;
            height: 33px;
        }

        #seacher input {
            width: 50%;
            background: #fff;
            border: none;
            border-radius: 5px;
            outline: none;
            box-sizing: border-box;
            padding: 0 10px;
            text-align: center;
            height: 33px;
        }

        #seacher button {
            width: 20%;
            border-radius: 5px;
            border: none;
            background: #4FC2A1;
            color: #fff;
            outline: none;
            height: 33px;
        }
</style>

@*</head>
    <body>
        </span>
        </span>
        </span>
        </span>
        </span>
        </span>*@

<div id="seacher">
    <input id="cAreaName" name="cAreaName" class="easyui-combotree" data-options="panelHeight:'auto',panelMaxHeight:'250px'" style="width:250px;height:22px;">
    <input id="BaseStationName" type="text" placeholder="基站名称">
    <button id="Search">搜索</button>
</div>
<div id="nav" class="swiper-container">
    <ul class="swiper-wrapper">
        <li class="swiper-slide active-nav" data-alarmtype="base" id="li_OutOfService">退服<span id="span_OutOfService">(0)</span></li>
        <li class="swiper-slide" data-alarmtype="base" id="li_PowerCut">停电<span id="span_PowerCut">(0)</span></li>
        <li class="swiper-slide" data-alarmtype="base" id="li_FSU">FSU离线<span id="span_FSU">(0)</span></li>
        <li class="swiper-slide" data-alarmtype="base" id="li_LowTension">低压<span id="span_LowTension">(0)</span></li>
        @*<li class="swiper-slide" data-alarmtype="gen" id="li_Generating">发电中<span id="span_Generating">(0)</span></li>
            <li class="swiper-slide" data-alarmtype="gen" id="li_SendOrders">已派单<span id="span_SendOrders">(0)</span></li>
            <li class="swiper-slide" data-alarmtype="gen" id="li_OilEngine">可用油机<span id="span_OilEngine">(0)</span></li>*@
    </ul>
</div>
<div id="tableHeader">
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th width="20%">区域</th>
            <th width="40%">基站名称</th>
            <th width="20%">基站级别</th>
            <th width="20%">告警时间</th>
        </tr>
    </table>
</div>
<div class="swiper-container" id="page">
    <div class="swiper-wrapper">
        <div class="swiper-slide slidepage">
            @*退服*@
        <table cellpadding="0" cellspacing="0" id="T_OutOfService">
            @*<tr>
                    <td>播州区</td>
                    <td>遵义市西平街上</td>
                    <td>普通基站</td>
                    <td>09-26 17:51</td>
                </tr>*@
        </table>
    </div>
    <div class="swiper-slide slidepage">
        @*停电*@
    <table cellpadding="0" cellspacing="0" id="T_PowerCut">
    </table>
</div>
<div class="swiper-slide slidepage">
    @*FSU离线*@
<table cellpadding="0" cellspacing="0" id="T_FSU">
</table>
</div>
<div class="swiper-slide slidepage">
    @*低压*@
<table cellpadding="0" cellspacing="0" id="T_LowTension">
</table>
</div>
@*<div class="swiper-slide slidepage" >@*发电中
        <table cellpadding="0" cellspacing="0" id="T_Generating">

        </table>
    </div>
    <div class="swiper-slide slidepage" >@*已派单
        <table cellpadding="0" cellspacing="0" id="T_SendOrders">

        </table>
    </div>
    <div class="swiper-slide slidepage" >@*可用油机
        <table cellpadding="0" cellspacing="0" id="T_OilEngine">

        </table>
    </div>*@
</div>
</div>
<script>

    var menuIndex = 0;//全局变量 菜单索引,默认为0,方便搜索时候使用

    bindAreaTree('#cAreaName', true);//初始化区域选择器

    changeListTitel(0, "", "", 0);//初始化打开告警列表退服数据

    winonloadCount();//初始化获得列表数据总数

    var dropload = null;
    var size = 10;

    //点击事件tap
    var myNav = new Swiper('#nav', {
        spaceBetween: 10,
        slidesPerView: 3,
        watchSlidesProgress: true,
        watchSlidesVisibility: true,
        on: {
            tap: function () {
                menuIndex = myNav.clickedIndex;
                myPage.slideTo(myNav.clickedIndex);
                changeListTitel(myNav.clickedIndex, "", "", 0);
            }
        }
    });

    //滑动事件
    var myPage = new Swiper('#page', {
        on: {
            slideChangeTransitionStart: function () {
                updateNavPosition()
            }
        }
    });

    //滑动时 标题选中对象进行改变
    function updateNavPosition() {
        $('#nav .active-nav').removeClass('active-nav');
        var activeNav = $('#nav .swiper-slide').eq(myPage.activeIndex).addClass('active-nav');
        changeListTitel(activeNav.index(), "", "", 0);
        menuIndex = (activeNav.index());
        if (!activeNav.hasClass('swiper-slide-visible')) {
            if (activeNav.index() >= myNav.activeIndex) {
                var thumbsPerNav = Math.floor(myNav.width / activeNav.width()) - 1
                myNav.slideTo(activeNav.index() - thumbsPerNav)
            } else {
                myNav.slideTo(activeNav.index())
            }
        }
    }

    //初始化获得列表数据总数
    function winonloadCount() {
        for (i = 0; i <= 6; i++) {
            changeListTitelCount(i, "", "", 0);
        }
    }

    //改变列表标题数量
    function changeListTitelCount(e, cAreaName, BaseStationName, search) {
        var urlApi = '/api/Info/GetAlarmListCount'
        switch (e) {
            case 0://退服
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 0, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    success: function (data) {
                        data = data.Data
                        $("#span_OutOfService").html("(" + data + ")");
                    }
                });
                break;
            case 1://停电
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 1, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    success: function (data) {
                        data = data.Data
                        $("#span_PowerCut").html("(" + data + ")");
                    }
                });
                break;
            case 2://FSU离线
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 2, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    success: function (data) {
                        data = data.Data
                        $("#span_FSU").html("(" + data + ")");
                    }
                });
                break;
            case 3://低压
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 3, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    success: function (data) {
                        data = data.Data
                        $("#span_LowTension").html("(" + data + ")");
                    }
                });
                break;
            //case 4://发电中
            //    $.ajax({
            //        url: '/Alarm/GetAlarmListCount',
            //        type: 'get',
            //        data: { menuIndex: 4, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        success: function (data) {
            //            $("#span_Generating").html("(" + data + ")");
            //        }
            //    });
            //    break;
            //case 5://已派单
            //    $.ajax({
            //        url: '/Alarm/GetAlarmListCount',
            //        type: 'get',
            //        data: { menuIndex: 5, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        success: function (data) {
            //            $("#span_SendOrders").html("(" + data + ")");
            //        }
            //    });
            //    break;
            //case 6://可用油机
            //    $.ajax({
            //        url: '/Alarm/GetAlarmListCount',
            //        type: 'get',
            //        data: { menuIndex: 6, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        success: function (data) {
            //            $("#span_OilEngine").html("(" + data + ")");
            //        }
            //    });
            //    break;
            default:
                break;
        }
    }

    var datalist = "";
    //改变列表标题跟加载数据
    function changeListTitel(e, cAreaName, BaseStationName, search) {
        var urlApi = '/api/Info/GetAlarmList';
        switch (e) {
            case 0://退服
                $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>基站级别</th><th width='15%'>告警时间</th></tr></table>");
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 0, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    beforeSend: function () {
                        openLoading();
                    },
                    success: function (data) {
                        closeLoading();
                        if (data == "[]") {
                            $("#T_OutOfService").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
                        } else {
                            var result = eval(data.Data);
                            result.forEach(function (d, i) {
                                datalist += "<tr><td>" + d.AreaName + "</td><td>" + d.BaseStationName + "</td><td>" + d.BaseStationLevel + "</td><td>" + d.AlarmOccTime + "</td></tr>";
                            });
                            //$("#T_OutOfService").html("");
                            $("#T_OutOfService").html(datalist);
                            changeListTitelCount(0, cAreaName, BaseStationName, search);
                            datalist = "";
                        }
                    }
                });
                break;
            case 1://停电
                $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>基站级别</th><th width='15%'>告警时间</th></tr></table>");
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 1, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    beforeSend: function () {
                        openLoading();
                    },
                    success: function (data) {
                        closeLoading();
                        if (data == "[]") {
                            $("#T_PowerCut").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
                        } else {
                            var result = eval(data.Data);
                            result.forEach(function (d, i) {
                                datalist += "<tr><td>" + d.AreaName + "</td><td>" + d.BaseStationName + "</td><td>" + d.BaseStationLevel + "</td><td>" + d.AlarmOccTime + "</td></tr>";
                            });
                            $("#T_PowerCut").html(datalist);
                            changeListTitelCount(1, cAreaName, BaseStationName, search);
                            datalist = "";
                        }
                    }
                });
                break;
            case 2://FSU离线
                //$("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>基站级别</th><th width='15%'>告警时间</th></tr></table>");
                //$("#T_FSU").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
                //changeListTitelCount(2, cAreaName, BaseStationName, search);
                $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>基站级别</th><th width='15%'>告警时间</th></tr></table>");
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 2, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    beforeSend: function () {
                        openLoading();
                    },
                    success: function (data) {
                        closeLoading();
                        if (data == "[]") {
                            $("#T_FSU").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
                        } else {
                            var result = eval(data.Data);
                            result.forEach(function (d, i) {
                                datalist += "<tr><td>" + d.AreaName + "</td><td>" + d.BaseStationName + "</td><td>" + d.BaseStationLevel + "</td><td>" + d.AlarmOccTime + "</td></tr>";
                            });
                            $("#T_FSU").html(datalist);
                            changeListTitelCount(2, cAreaName, BaseStationName, search);
                            datalist = "";
                        }
                    }
                });
                break;
            case 3://低压
                $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>基站级别</th><th width='15%'>告警时间</th></tr></table>");
                $.ajax({
                    url: urlApi,
                    type: 'get',
                    data: { menuIndex: 3, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
                    beforeSend: function () {
                        openLoading();
                    },
                    success: function (data) {
                        closeLoading();
                        if (data == "[]") {
                            $("#T_LowTension").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
                        } else {
                            var result = eval(data.Data);
                            result.forEach(function (d, i) {
                                datalist += "<tr><td>" + d.AreaName + "</td><td>" + d.BaseStationName + "</td><td>" + d.BaseStationLevel + "</td><td>" + d.AlarmOccTime + "</td></tr>";
                            });
                            $("#T_LowTension").html(datalist);
                            changeListTitelCount(3, cAreaName, BaseStationName, search);
                            datalist = "";
                        }
                    }
                });
                break;
            //case 4://发电中
            //    $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>基站编码</th></tr></table>");
            //    $.ajax({
            //        url: '/Alarm/GetAlarmList',
            //        type: 'get',
            //        data: { menuIndex: 3, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        beforeSend: function () {
            //            openLoading();
            //        },
            //        success: function (data) {
            //            closeLoading();
            //            if (data == "[]") {
            //                $("#T_Generating").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
            //            } else {
            //                var result = eval(data);
            //                result.forEach(function (d, i) {
            //                    datalist += "<tr><td>" + d.AreaName + "</td><td>" + d.BaseStationName + "</td><td>" + d.BaseStationTTCode + "</td></tr>";
            //                });
            //                $("#T_Generating").html(datalist);
            //                changeListTitelCount(4, cAreaName, BaseStationName, search);
            //                datalist = "";
            //            }
            //        }
            //   });

            //$("#T_Generating").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
            //changeListTitelCount(4, cAreaName, BaseStationName, search);
            //break;
            //case 5://已派单
            //    $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>派单数量</th></tr></table>");
            //    $.ajax({
            //        url: '/Alarm/GetAlarmList',
            //        type: 'get',
            //        data: { menuIndex: 5, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        beforeSend: function () {
            //            openLoading();
            //        },
            //        success: function (data) {
            //            closeLoading();
            //            if (data == "[]") {
            //                $("#T_SendOrders").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
            //            } else {
            //                var result = eval(data);
            //                result.forEach(function (d, i) {
            //                    var Count = (d.Count) == 0 ? (d.Count) : ("<font color='red'>" + (d.Count) + "</font>");//数字不为0调整颜色
            //                    datalist += "<tr><td>" + d.AreaName + "</td><td>" + Count + "</td></tr>";
            //                });
            //                $("#T_SendOrders").html(datalist);
            //                changeListTitelCount(5, cAreaName, BaseStationName, search);
            //                datalist = "";
            //            }
            //        }
            //    });
            //    break;

            //正式
            //case 5://已派单
            //    $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>基站名称</th><th width='18%'>工单号</th></tr></table>");
            //    $.ajax({
            //        url: '/Alarm/GetAlarmList',
            //        type: 'get',
            //        data: { menuIndex: 5, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        beforeSend: function () {
            //            openLoading();
            //        },
            //        success: function (data) {
            //            closeLoading();
            //            if (data == "[]") {
            //                $("#T_SendOrders").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
            //            } else {
            //                var result = eval(data);
            //                result.forEach(function (d, i) {
            //                    datalist += "<tr><td>" + d.AreaName + "</td><td>" + d.BaseStation + "</td><td>" + d.BillNO + "</td></tr>";
            //                });
            //                $("#T_SendOrders").html(datalist);
            //                changeListTitelCount(5, cAreaName, BaseStationName, search);
            //                datalist = "";
            //            }
            //        }
            //    });
            //    break;
            //case 6://可用油机
            //    $("#tableHeader").html("<table cellpadding='0' cellspacing='0'><tr><th width='17%'>区域</th><th width='40%'>可用数量</th></tr></table>");
            //    $.ajax({
            //        url: '/Alarm/GetAlarmList',
            //        type: 'get',
            //        data: { menuIndex: 6, cAreaName: cAreaName, BaseStationName: BaseStationName, search: search },
            //        beforeSend: function () {
            //            openLoading();
            //        },
            //        success: function (data) {
            //            closeLoading();
            //            if (data == "[]") {
            //                $("#T_OilEngine").html("<p style='width: 100%;height: 45px;display: block;line-height: 45px;text-align: center;color:#757575;font-size:0.7rem;'>0条记录</p>");
            //            } else {
            //                var result = eval(data);
            //                result.forEach(function (d, i) {
            //                    var Count = (d.Count) == 0 ? (d.Count) : ("<font color='red'>" + (d.Count) + "</font>");//数字不为0调整颜色
            //                    datalist += "<tr><td>" + d.AreaName + "</td><td>" + Count + "</td></tr>";
            //                });
            //                $("#T_OilEngine").html(datalist);
            //                changeListTitelCount(6, cAreaName, BaseStationName, search);
            //                datalist = "";
            //            }
            //        }
            //    });
            //    break;
            default:
                break;
        }
    }

    function TreeData() {
        $.ajax({
            url: '/api/Info/GetAreas',
            type: 'get',
            success: function (data) {
                var result = eval(data.Data);
                toTreeData(result, "AreaCode", "ParentCode", "AreaName");

            }
        })
    }

    //搜索按钮
    $("#Search").click(function () {
        var cAreaName = $('#cAreaName').combotree("getValue");//区域AreaCode
        var BaseStationName = $("#BaseStationName").val().trim();//搜索框
        changeListTitel(menuIndex, cAreaName, BaseStationName, 1);
    })


    /**
    绑定区域树下拉列表
    param {} control             #控件ID
    param {} bSelectFirstNode    是否选中绑定后的第一项
    returns {}
     */
    function bindAreaTree(control, bSelectFirstNode, onSuccess, onSelectFun) {
        bindCombotreeFromApi(
            control, //combotree-ID
            '/api/Info/GetAreas', //api
            'AreaCode', //data的ID字段
            'ParentCode',     //data的父ID字段
            'AreaName',//data中用于显示的Text字段
            onSelectFun, onSuccess, bSelectFirstNode
        );
    }

    function bindCombotreeFromApi(control, api, idField, pidFiled, textField, selectFun, onLoadSuccess, bSelectFirstNode, bLocal, bSync) {
        try {
            $.ajax({
                url: api,
                type: 'get',
                success: function (d) {
                    //var array = JSON.parse(d)
                    var array = eval(d.Data);
                    bindCombotreeFromData(control, array, idField, pidFiled, textField, selectFun, onLoadSuccess, bSelectFirstNode, bLocal);
                }
            });
        } catch (e) {
            alert(e.message);
        }
    }


    function bindCombotreeFromData(control, data, idField, pidFiled, textField, selectFun, onLoadSuccess, bSelectFirstNode, bLocal) {
        try {
            data = data || [];//避免data=null引起异常
            data = copyProperty(data, [idField, pidFiled, textField], ['id', 'pid', 'text'], true); //转化为easyui - tree需要的id属性
            if (bLocal && lan) { //本地化处理
                lan.localObjects(data, 'text');
            }
            data = toTreeData(data, "id", "pid", "children"); //转化为easyui-tree需要的json-tree格式
            //data = copyProperty(data, [  textField], [ 'text'], true); //转化为easyui - tree需要的id属性
            //data = toTreeData(data, idField, pidFiled, "children"); //转化为easyui-tree需要的json-tree格式

            $(control)
                .combotree({
                    data: data
                });

            if (typeof (selectFun) != 'undefined' && selectFun != null)
                $(control)
                    .combotree({
                        onSelect: selectFun
                    });
            if (typeof (onLoadSuccess) != 'undefined' && onLoadSuccess != null)
                $(control)
                    .combotree({
                        onLoadSuccess: onLoadSuccess
                    });
            if (bSelectFirstNode && data && data[0]) {
                var firstValue = data[0][idField];
                $(control).combotree("setValue", firstValue);
            }
        } catch (err) {
            alert(err.message);
        }
    }

    /*
    json格式转树状结构
    param   {json}      json数据
    param   {String}    id的字符串
    param   {String}    父id的字符串
    param   {String}    children的字符串
    */
    toTreeData = function (a, idStr, pidStr, childrenStr) {
        var r = [], hash = {}, len = (a || []).length;
        for (var i = 0; i < len; i++) {
            hash[a[i][idStr]] = a[i];
        }
        for (var j = 0; j < len; j++) {
            var aVal = a[j], hashVP = hash[aVal[pidStr]];
            if (hashVP) {
                !hashVP[childrenStr] && (hashVP[childrenStr] = []);
                hashVP[childrenStr].push(aVal);
            } else {
                r.push(aVal);
            }
        }
        return r;
    };

    copyProperty = function (obj, sourcePropertyName, newPropertyName, overWrite) {
        if (obj instanceof Array || Object.prototype.toString.call(obj) === "[object Array]") {
            for (var k in obj)
                copyProperty(obj[k], sourcePropertyName, newPropertyName);
        }
        else if (typeof obj === 'object') {
            if (sourcePropertyName instanceof Array || Object.prototype.toString.call(sourcePropertyName) === "[object Array]") {
                for (var i in sourcePropertyName) {
                    copyProperty(obj, sourcePropertyName[i], newPropertyName[i]);
                }
            }
            else if (typeof sourcePropertyName === 'string') {
                if ((obj[newPropertyName] && overWrite) || (!obj[newPropertyName]))
                    obj[newPropertyName] = obj[sourcePropertyName];
            }
        }
        return obj;
    };

</script>

